package com.CityHunter.Request;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.util.Log;

import com.CityHunter.Common.Http;
import com.CityHunter.DataObject.Schedule;
import com.CityHunter.DataObject.SchedulePoint;

public class CreateScheduleRequest {

	private static String createSchedulePage;

	public CreateScheduleRequest(String createSchedulePage) {
		this.createSchedulePage = createSchedulePage;
	}

	public static int createSchedule(int userid, Schedule schedule) {

		int result = 0;
		int scheduleId = 0;

		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userid", Integer.toString(userid)));
		params.add(new BasicNameValuePair("startTime", schedule.getStartTime()
				.toString()));
		params.add(new BasicNameValuePair("endTime", schedule.getEndTime()
				.toString()));
		params.add(new BasicNameValuePair("description", schedule
				.getDescription()));

		// Send create group Request
		Http http = new Http();
		Log.d("cgrp_Debug", "Sending Request : " + createSchedulePage);
		JSONArray jsonResponse = http.sendRequest(createSchedulePage, params);
		try {
			scheduleId = Integer.parseInt(jsonResponse.getJSONObject(0)
					.getString("scheduleID"));
			Log.d("Debug", "Response Result : " + scheduleId);
		} catch (Exception e) {
			Log.e("Error", e.getMessage().toString());
		}

		if (scheduleId > 0) {
			result = 1;
		}

		// Declare NameValuePair to store the data
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("numOfSP", Integer.toString(schedule
				.size())));

		// loop all schedulePoint into params
		for (int i = 0; i < schedule.size(); i++) {

			SchedulePoint sp = schedule.get(i);

			params.add(new BasicNameValuePair("scheduleID" + i, Integer
					.toString(scheduleId)));
			params.add(new BasicNameValuePair("pointNumber" + i, Integer
					.toString(sp.getOrder())));
			params.add(new BasicNameValuePair("time" + i, sp.getTime()
					.toString()));
			params.add(new BasicNameValuePair("longitude" + i, Integer
					.toString(sp.geoPoint().getLongitudeE6())));
			params.add(new BasicNameValuePair("latitude" + i, Integer
					.toString(sp.geoPoint().getLatitudeE6())));
			params.add(new BasicNameValuePair("description" + i, sp
					.getDescription()));

		}

		// Send create group Request
		http = new Http();
		Log.d("cgrp_Debug", "Sending Request : " + createSchedulePage);
		jsonResponse = http.sendRequest(createSchedulePage, params);
		try {
			scheduleId = Integer.parseInt(jsonResponse.getJSONObject(0)
					.getString("scheduleID"));
			Log.d("Debug", "Response Result : " + scheduleId);
		} catch (Exception e) {
			Log.e("Error", e.getMessage().toString());
		}

		return result;
	}

}
